package yz.cp.back.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.web.PageableDefault;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import yz.cp.back.admin.service.DictManage;
import yz.cp.back.common.base.BaseController;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.s.entity.SysDict;
import yz.cp.back.s.entity.SysDictData;
import yz.cp.back.s.service.ISysDictDataService;
import yz.cp.back.s.service.ISysDictService;

import java.util.List;


/**
 * 字典数据管理接口
 * @author xukk
 */
@Slf4j
@RestController
@RequestMapping("/web/dictData")
@CacheConfig(cacheNames = "dictData")
@Transactional
public class DictDataController extends BaseController {

    @Autowired
    private ISysDictService dictService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private DictManage dictManage;

    /**
     * 多条件分页获取用户列表
     * @param dictData
     * @param pageVo
     * @return
     */
    @RequestMapping(value = "/getByCondition",method = RequestMethod.GET)
    public ApiResult getByCondition(@ModelAttribute SysDictData dictData,
                                    @PageableDefault Pageable pageable){

       return renderOk().add(dictManage.findByCondition(dictData, pageable));
    }

    /**
     * 通过类型获取
     * @param type
     * @return
     */
    @RequestMapping(value = "/getByType/{type}",method = RequestMethod.GET)
    @Cacheable(key = "#type")
    public ApiResult getByType(@PathVariable String type){

        SysDict dict = dictService.getOne(new QueryWrapper<SysDict>().lambda().eq(SysDict::getType,type));
        if (dict == null) {
            return renderError("字典类型Type不存在");
        }
        List<SysDictData> list = dictDataService.list(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictId,dict.getId()));
        return renderOk().add(list);
    }

    /**
     * 添加
     * @param dictData
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ApiResult add(@ModelAttribute SysDictData dictData){

        SysDict dict = dictService.getById(dictData.getDictId());
        if (dict == null) {
            return renderError("字典类型id不存在");
        }
        dictDataService.save(dictData);
        // 删除缓存
        redisTemplate.delete("dictData::"+dict.getType());
        return renderOk("添加成功");
    }

    /**
     * 编辑
     * @param dictData
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ApiResult edit(@ModelAttribute SysDictData dictData){

        dictDataService.updateById(dictData);
        // 删除缓存
        SysDict dict = dictService.getById(dictData.getDictId());
        redisTemplate.delete("dictData::"+dict.getType());
        return renderOk("编辑成功");
    }

    /**
     * 批量通过id删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delByIds/{ids}",method = RequestMethod.DELETE)
    public ApiResult delByIds(@PathVariable String[] ids){
        for(String id : ids){
            SysDictData dictData = dictDataService.getById(id);
            SysDict dict = dictService.getById(dictData.getDictId());
            dictDataService.removeById(id);
            // 删除缓存
            redisTemplate.delete("dictData::"+dict.getType());
        }
        return renderOk("批量通过id删除数据成功");
    }
}
